package edu.ycxy.poetry.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ShiWen;
import edu.ycxy.poetry.entity.Writer;
import edu.ycxy.poetry.mapper.ShiWenMapper;
import edu.ycxy.poetry.service.IShiWenService;
import edu.ycxy.poetry.vo.AddPoemVo;
import edu.ycxy.poetry.vo.UpdatePoemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Service
public class ShiWenServiceImpl extends ServiceImpl<ShiWenMapper, ShiWen> implements IShiWenService {

    @Autowired
    private ShiWenMapper shiWenMapper;


    //根据古诗的时间,筛选倒序的42首古诗
    @Override
    public List<ShiWen> FindPoemsByTime() {
        return shiWenMapper.findByTime();
    }

    //根据作者姓名统计作者的诗词总数
    @Override
    public Integer CountForWriter(String name) {
        return shiWenMapper.findCountForWriter(name);
    }

    //根据古诗名查询古诗的整首诗
    @Override
    public ShiWen findPoemByName(String poemName) {
        return shiWenMapper.findPoemByName(poemName);
    }

    //根据古诗名查询古诗作者的生平
    @Override
    public Writer findWriterByPoemName(String poemName) {
        return shiWenMapper.findWriteByPoemName(poemName);
    }

    //查询古诗表中所有古诗词数据
    //使用mybatis-plus中的分页查询进行查询
    @Override
    public PageInfo<ShiWen> findAllPoems(Integer pageNum, Integer pageSize) {
        /**
         * startPage 方法的参数我们有两个,第一个是要查询第几页,如果想要查询第一页写1
         * 第二个参数就是当前页最多显示多少条
         * 这个代码一旦编写,会在下一次查询的时候自动添加 limit 关键字,以及具体的区间
         */
        PageHelper.startPage(pageNum,pageSize);
        List<ShiWen> shiWens = shiWenMapper.findAllShiWen();
        PageInfo pageInfo = new PageInfo(shiWens);
        //返回查询结果
        return pageInfo;
    }

    //根据古诗的id查询古诗的详细信息
    @Override
    public ShiWen selectById(Integer id) {
        return shiWenMapper.selectById(id);
    }

    //根据作者的姓名查询作者的所有诗词
    @Override
    public PageInfo<ShiWen> selectListByAuthor(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShiWen> shiWens = shiWenMapper.selectListByAuthor(name);
        PageInfo pageInfo = new PageInfo(shiWens);
        return pageInfo;
    }

    /**
     * 根据古诗的类型查询古诗的信息，<唐诗三百首> 等 , 模糊查询，使用CONCAT关键字
     * @param typeName 类型名 -> <唐诗三百首>
     * @return 返回pageInfo对象
     */
    @Override
    public PageInfo<ShiWen> findListByTypeName(String typeName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShiWen> listByTypeName = shiWenMapper.findListByTypeName(typeName);
        PageInfo pageInfo = new PageInfo(listByTypeName);
        return pageInfo;
    }

    /**
     * 新增古诗词
     * @param addPoemVo 表单vo类
     * @return
     */
    @Override
    public String addPoem(AddPoemVo addPoemVo) {
        // 通过诗词的正文查询该古诗是否存在
        ShiWen oneByBody = shiWenMapper.findOneByBody(addPoemVo.getBody());
        // 说明该诗词不存在
        if (oneByBody == null){
            shiWenMapper.addPoem(addPoemVo);
            return "插入成功！！";
        }
        return "该古诗已存在";
    }

    /**
     * 对诗词进行多字段匹配的模糊查询，并对查询出的结果进行分页展示
     * @param search 搜索框的值
     * @param pageNum  分页页码
     * @param pageSize 每页条数
     * @return pageInfo
     */
    @Override
    public PageInfo<ShiWen> findListBySearchToLike(String search, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShiWen> listBySearchToLike = shiWenMapper.findListBySearchToLike(search);
        PageInfo pageInfo = new PageInfo(listBySearchToLike);
        return pageInfo;
    }

    @Override
    public void deletePoemById(Integer id) {
        shiWenMapper.deletePoemById(id);
    }

    @Override
    public Integer updatePoemById(UpdatePoemVo updatePoemVo) {
        Integer integer = shiWenMapper.updatePoemById(updatePoemVo);
        if (integer == 1){
            return 1;
        }else {
            return 2;
        }
    }
}
